package com.zidian.leader.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.zidian.leader.R;
import com.zidian.leader.api.Constant;

import java.util.concurrent.TimeUnit;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Subscription;
import rx.functions.Action1;

/**
 * 主界面菜单adapter
 * Created by GongCheng on 2016/11/17.
 */

public class MainItemTypeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private OnItemClickListener listener;
    private Subscription subscription;
    private static final int[] MENU_ITEM_IDS = {
            R.drawable.ic_student_statistics,
            R.drawable.ic_teaching_quality,
            R.drawable.ic_enrol_adaptation,
            R.drawable.ic_employment_quality,
            R.drawable.ic_ranking_list,
            R.drawable.ic_course_level,
            R.drawable.ic_club_activities,
            R.drawable.ic_student_improvement,
            R.drawable.ic_teacher_improvement,
            R.drawable.ic_students_quality,
            R.drawable.ic_train,
            R.drawable.ic_others
            };

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_questionnaire_type, parent, false);

        return new ItemTypeHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ItemTypeHolder) {
            ((ItemTypeHolder) holder).itemTypeIv.setImageResource(MENU_ITEM_IDS[position]);
            ((ItemTypeHolder) holder).itemTypeTv.setText(Constant.MENU_ITEM_LIST.get(position));

        }
    }

    @Override
    public int getItemCount() {
        return Constant.MENU_ITEM_LIST.size();
    }

    public interface OnItemClickListener {
        void onItemClick(String type);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    /**
     * 解除绑定
     */
    public void unSubscribeView() {
        if (subscription != null && subscription.isUnsubscribed()) {
            subscription.unsubscribe();
        }
    }
    class ItemTypeHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.item_type_iv)
        ImageView itemTypeIv;
        @Bind(R.id.item_type_tv)
        TextView itemTypeTv;

        public ItemTypeHolder(final View itemView) {
            super(itemView);

            ButterKnife.bind(this, itemView);
            subscription = RxView.clicks(itemView)
                    .throttleFirst(2000, TimeUnit.MILLISECONDS)
                    .subscribe(new Action1<Void>() {
                        @Override
                        public void call(Void aVoid) {
                            if (listener != null) {
                                listener.onItemClick(Constant.MENU_ITEM_LIST.get(getAdapterPosition()));
                            }
                        }
                    });
        }

    }
}
